package com.yxp.util;

import java.util.List;

/**
 * @author : yang.xp
 * @date : 2021/9/4 16:24
 * @description : 随便重写测测测
 */
public class Test {

    public static void main(String[] args) {
        ListNode listNode = ListNode.initListNode(new int[]{1, 2, 3, 4, 5});
        System.out.println(Test.findRepeatNumber3(new int[]{1, 1, 3, 4, 5}));
    }

    public ListRandomNode copyRandomList(ListRandomNode listNode) {
        // 1 复制 next
        ListRandomNode listRandomNode = listNode;
        while (listRandomNode != null) {
            ListRandomNode newRandomNode = new ListRandomNode(listRandomNode.val);
            newRandomNode.next = listRandomNode.next;
            listRandomNode.next = newRandomNode;
            listRandomNode = newRandomNode.next;
        }
        // 2 指向random
        listRandomNode = listNode;
        while (listRandomNode != null) {
            ListRandomNode tempRandomNode = listRandomNode.random;
            if (tempRandomNode != null) {
                listRandomNode.next.random = tempRandomNode.next;
            }
            listRandomNode = listRandomNode.next.next;
        }
        // 3 分离
        listRandomNode = listNode;
        ListRandomNode resultListRandomNode = listRandomNode.next;
        ListRandomNode curNode = listRandomNode;
        while (curNode != null) {
            curNode.next = resultListRandomNode.next;
            resultListRandomNode.next = curNode.next.next;
            curNode = resultListRandomNode.next;
        }
        return resultListRandomNode;
    }


    public String replaceSpace(String s) {
        StringBuilder stringBuilder = new StringBuilder();
        char[] chars = s.toCharArray();
        for (char ch : chars) {
            if (ch == ' ') {
                stringBuilder.append("%20");
            } else {
                stringBuilder.append(ch);
            }
        }
        return stringBuilder.toString();
    }

    public String reverseLeftWords(String s, int n) {
        char[] chars = s.toCharArray();
        char[] result = new char[s.length()];
        int j = 0;
        for (int i = n; i < chars.length; i++) {
            result[j++] = chars[i];
        }
        for (int i = 0; i < n; i++) {
            result[j++] = chars[i];
        }
        return new String(result);
    }

    public static int findRepeatNumber3(int[] nums) {
        for (int i = 0; i < nums.length; i++) {
            while (nums[i] != i) {
                if(nums[i] == nums[nums[i]]){
                    return nums[i];
                }
                int temp = nums[i];
                nums[i] = nums[temp];
                nums[temp] = temp;
            }
        }
        return -1;
    }

}
